/*
 * @(#)YamlPropUtil.java 1.0 2019年10月24日上午10:04:04
 *
 * Saturn Framework Co.Limt
 * Copyright (c) 2012-2019 tanks, Inc. All rights reserved.
 */
package com.saturn.config.server.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Properties;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.InputStreamResource;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <dl>
 * <dt><b>Title:</b></dt>
 * <dd>none</dd>
 * <dt><b>Description:</b></dt>
 * <dd>
 * <p>
 * none</dd>
 * </dl>
 *
 * @author tanks
 * @version 1.0, 2019年10月24日
 * @since config-center-admin
 * 
 */
@Slf4j
public class YamlPropUtil {

	/**
	 * yaml to properties属性格式
	 * @param path
	 */
	public static String yml2PropertiesText(String content) throws IOException {
		Properties props = yml2Properties(content);
		StringBuffer result = new StringBuffer();
		for(Object key:props.keySet()) {
			String value = props.getProperty((String)key);
			result.append(key).append("=").append(value).append("\r\n");
		}
		return result.toString();
	}

	/**
	 * yaml内容 to properties属性格式
	 * @param path
	 * @throws IOException 
	 */
	public static Properties yml2Properties(String content) throws IOException {
		InputStream input = null;
		try {
			input = new ByteArrayInputStream(content.getBytes());
			return YamlPropUtil.yml2Properties(input);
		}finally {
			if( input!=null ) {
				input.close();
			}
		}
	}
	
	/**
	 * Yaml文件转行properties
	 * @param input
	 * @return
	 */
	public static Properties yml2Properties(InputStream input) throws IOException {
		YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
		InputStreamResource inputStreamResource = new InputStreamResource(input);
		yamlPropertiesFactoryBean.setResources(inputStreamResource);
		return yamlPropertiesFactoryBean.getObject();
	}
	
	/**
	 * Properties to yaml format
	 * @param props
	 * @return
	 */
	public static String properties2Yaml(Properties props) {
		StringBuffer content = new StringBuffer();
		for(Object key:props.keySet()) {
			String value = props.getProperty((String)key);
			content.append(key).append("=").append(value).append("\r\n");
		}
		return properties2Yaml(content.toString());
	}
	
	/**
	 * 把Properties文件转成yaml文件格式
	 * @param content
	 * @return
	 */
	public static String properties2Yaml(String content) {
		JsonParser parser = null;
		JavaPropsFactory factory = new JavaPropsFactory();
		YAMLFactory yamlFactory = new YAMLFactory();
		StringWriter writer = new StringWriter();
		YAMLGenerator generator = null;
		String yaml = StrUtil.EMPTY;
		try {
			parser = factory.createParser(content);
			generator = yamlFactory.createGenerator(writer);
			JsonToken token = parser.nextToken();
			while (token != null) {
				//log.info(token.toString());
				if (JsonToken.START_OBJECT.equals(token)) {
					generator.writeStartObject();
				} else if (JsonToken.FIELD_NAME.equals(token)) {
					generator.writeFieldName(parser.getCurrentName());
				} else if (JsonToken.VALUE_STRING.equals(token)) {
					generator.writeString(parser.getText());
				} else if (JsonToken.END_OBJECT.equals(token)) {
					generator.writeEndObject();
				}else if (JsonToken.START_ARRAY.equals(token)) {
					generator.writeStartArray();
				}else if (JsonToken.END_ARRAY.equals(token)) {
					generator.writeEndArray();
				}
				token = parser.nextToken();
			}
			generator.flush();
			yaml = writer.toString();
		} catch (Exception ex) {
			log.error("Properties格式转YAML格式解析失败",ex);
			throw new RuntimeException(ex);
		}finally {
			if( generator!=null ) {
				try {
					generator.close();
					generator = null;
				} catch (IOException e) {
					//ignore
				}
			}
			if( parser!=null ) {
				try {
					parser.close();
					parser = null;
				} catch (IOException e) {
					//ignore
				}
			}
			if( writer!=null ) {
				try {
					writer.close();
					writer = null;
				} catch (IOException e) {
					//ignore
				}
			}
			yamlFactory = null;
			factory = null;
		}
		return yaml;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		//yml2Properties("C:\\Users\\tanks\\Desktop\\src\\temp\\bootstrap.yml");
		/*
		 * String content = FileUtil.readAsString(new
		 * File("C:\\Users\\tanks\\Desktop\\src\\temp\\bootstrap.yml")); String text =
		 * yml2PropertiesText(content); System.out.println(text);
		 * System.out.println("----------------------------------"); String content1 =
		 * properties2Yaml(text); System.out.println(content1);
		 */
		TextEncryptor encryptor = Encryptors.text("123456", "deadbeef");
		String t1 = encryptor.encrypt("23");
		System.out.println(t1);
		System.out.println(encryptor.decrypt(t1));
	}

}
